package com.rljk.service.impl;

import java.util.List;
import javax.annotation.Resource;

import com.rljk.common.consts.WealthTypeEnum;
import com.rljk.redis.RedisService;
import com.rljk.service.WealthManager;
import com.rljk.utils.DecimalUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rljk.common.dao.DaoSupport;
import com.github.pagehelper.PageHelper;
import com.rljk.domain.common.PageData;
import org.springframework.transaction.annotation.Transactional;

/** 
 * 说明： 财富表
 * 创建人：zhaozhenhua
 * 创建时间：2018-07-11
 * @version
 */
@Service("wealthService")
@Slf4j
public class WealthService implements WealthManager {

	@Resource(name = "daoSupport")
	private DaoSupport dao;

	@Resource(name = "assetsService")
	private AssetsService assetsService;

	@Autowired
	RedisService redisService;
	
	/**新增
	 * @param pd
	 * @throws Exception
	 */
	public void save(PageData pd)throws Exception{
		dao.save("WealthMapper.save", pd);
	}
	
	/**删除
	 * @param pd
	 * @throws Exception
	 */
	public void delete(PageData pd)throws Exception{
		dao.delete("WealthMapper.delete", pd);
	}
	
	/**修改
	 * @param pd
	 * @throws Exception
	 */
	public void edit(PageData pd)throws Exception{
		dao.update("WealthMapper.edit", pd);
	}
	
	/**分页列表
	 * @param currentPage
	 * @param pageSize
	 * @param pd
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<PageData> listPage(int currentPage,int pageSize,PageData pd)throws Exception{
	    PageHelper.startPage(currentPage,pageSize);
		return (List<PageData>)dao.findForList("WealthMapper.listAll", pd);
	}
	
	/**列表(全部)
	 * @param pd
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<PageData> listAll(PageData pd)throws Exception{
		return (List<PageData>)dao.findForList("WealthMapper.listAll", pd);
	}
	
	/**通过id获取数据
	 * @param pd
	 * @throws Exception
	 */
	public PageData findById(PageData pd)throws Exception{
		return (PageData)dao.findForObject("WealthMapper.findById", pd);
	}
	
	/**批量删除
	 * @param ArrayDATA_IDS
	 * @throws Exception
	 */
	public void deleteAll(String[] ArrayDATA_IDS)throws Exception{
		dao.delete("WealthMapper.deleteAll", ArrayDATA_IDS);
	}

	/**
	 * 获取财富（能量值，XW链，积分，幸运钻）
	 */
	@Override
	public List<PageData> getWealthValue(PageData pd) throws Exception {
		return (List<PageData>)dao.findForList("WealthMapper.getWealthValue", pd);
	}

	/**
	 * 获取宝石矿每天产生的宝石
	 */
	@Override
	public List<String> getXWStone(String USER_ID) throws Exception {
		return redisService.mget(USER_ID);
	}

	@Override
	@Transactional
	public Object clickXWStone(PageData pd) throws Exception {
		String token = redisService.get(pd.getString("TOKEN"));
		pd.put("USER_ID",token);
		String WEALTH_VALUE = redisService.get(token+"_"+pd.getString("WEALTH_ID"));
		log.info("WEALTH_VALUE:"+WEALTH_VALUE);
		pd.put("WEALTH_VALUE",WEALTH_VALUE);
		pd.put("WEALTH_TYPE", WealthTypeEnum.XW.getCode());
		pd.put("GET_TYPE",1);
		save(pd);
		//删除redis中wealth
		redisService.del(token+"_"+pd.getString("WEALTH_ID"));
		//总的xw链增加
		PageData asset = assetsService.findById(pd);
		double xw_value = asset.getDouble("XW_VALUE");
		double xw = DecimalUtils.add(xw_value, Double.parseDouble(WEALTH_VALUE));
		pd.put("XW_VALUE",xw);
		assetsService.edit(pd);
		return xw;
	}


}

